package com.tcm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tcm.common.enums.CachePrefix;
import com.tcm.config.AppProperties;
import com.tcm.entity.ItemLabel;
import com.tcm.entity.ItemStudent;
import com.tcm.entity.bo.UserCacheBo;
import com.tcm.mapper.*;
import com.tcm.service.*;
import com.tcm.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/9/28 14:25
 */
@Service
@RequiredArgsConstructor
public class StatisticsServiceImpl implements StatisticsService {

    private final RedisUtil redisUtil;

    private final ItemStudentService itemStudentService;

    private final ItemLabelMapper itemLabelMapper;

    private final BookMapper bookMapper;

    private final StudentService studentService;

    private final TestPaperService testPaperService;

    private final ClozeItemService clozeItemService;

    private final SubjectItemService subjectItemService;

    private final LearningTimeMapper learningTimeMapper;

    private final StudentMapper studentMapper;

    private final TeacherMapper teacherMapper;

    private final RoleService roleService;

    /**
     * 获取学生学习统计信息
     * @param currUserId 当前用户ID
     * @return 学生学习统计信息
     */
    @Override
    public Map<String, Integer> getStuLearningStatistics(Long currUserId) {
        // 获取用户信息
        UserCacheBo userCacheBo = redisUtil.getCacheObject(CachePrefix.PC_USER_TOKEN.getCacheKey(String.valueOf(currUserId)));

        // 获取当前用户做题记录
        List<ItemStudent> itemStudentList = itemStudentService.list(new LambdaQueryWrapper<ItemStudent>().eq(ItemStudent::getAccount, userCacheBo.getAccount()));
        List<Long> itemIds = itemStudentList.stream().map(ItemStudent::getItemId).collect(Collectors.toList());
        Integer labelCount = itemLabelMapper.selectCount(new LambdaQueryWrapper<ItemLabel>().in(!CollectionUtils.isEmpty(itemIds), ItemLabel::getItemId, itemIds).apply(CollectionUtils.isEmpty(itemIds), "1 != 1"));
        return buildResultMap(itemStudentList, itemIds, labelCount);
    }

    @Override
    public Map<String, Integer> getIndexStatistic() {
        Map<String, Integer> map = new HashMap<>();
        map.put("stuNum", studentService.count());
        map.put("testPaperNum", testPaperService.count());
        map.put("topicNum", clozeItemService.count() + subjectItemService.count());
        map.put("averageLearningTime", learningTimeMapper.selectAverage());
        map.put("averageScore", studentMapper.selectScoreAverage());
        return map;
    }

    private Map<String, Integer> buildResultMap(List<ItemStudent> itemStudents, List<Long> itemIds, Integer labelCount) {
        Map<String, Integer> res = new HashMap<>();
        res.put("subjectCount", (int) itemStudents.stream().filter(itemStudent -> itemStudent.getItemType().equals(AppProperties.ITEM_TYPE_SUBJECT)).count());
        res.put("clozeCount", (int) itemStudents.stream().filter(itemStudent -> itemStudent.getItemType().equals(AppProperties.ITEM_TYPE_CLOZE)).count());
        res.put("knowledgePointCount", labelCount);
        res.put("chapterCount", !CollectionUtils.isEmpty(itemIds) ? bookMapper.selectLearntBookCountByItemIds(itemIds) : 0);
        return res;
    }

}
